import QtQuick 2.15
import QtQuick.Controls 2.15
import './controls'

Rectangle {
    id:loginMain
    signal loginSuccessed()
    function setUserNamePwd(uname,pwd){
        loginView.setUserNamePwd(uname,pwd)
    }
    Item {
        anchors.fill: parent
        function setUserNamePwd(uname,pwd){
            textUserName.text=uname
            textPassword.text=pwd
        }
        Column{
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 15
            Column{
                spacing: 15
                Rectangle{
                    width: 320
                    height: 43
                    color: '#4cC2C2C2'
                    radius:5
                    Image {
                        source: "qrc:/images/login/user.png"
                        anchors.verticalCenter: parent.verticalCenter
                        x:15
                    }
                    TextField{
                        id:textUserName
                        x:50
                        height: parent.height-2
                        width: parent.width - x - 4
                        font.pixelSize: 14
                        maximumLength:30
                        color: '#333333'
                        text: {
                            let user= CommonHelper.getValue("lastUser")
                            if(user && user.length > 0)
                            {
                                return user
                            }
                            return ''
                        }
                        placeholderText: qsTr('请输入账号')
                        anchors.verticalCenter: parent.verticalCenter
                        selectByMouse: true
                        background: Item{}
                    }
                }
                Rectangle{
                    width: 320
                    height: 43
                    color: '#4cC2C2C2'
                    radius:5
                    Image {
                        width: 22
                        height: 22
                        source: "qrc:/images/login/password.png"
                        anchors.verticalCenter: parent.verticalCenter
                        x:15
                    }
                    TextField{
                        id:textPassword
                        x:50
                        height: parent.height-2
                        width: parent.width - x - 4
                        maximumLength:30
                        font.pixelSize: 14
                        color: '#333333'
                        text: {
                            let pwd= CommonHelper.getValue("lastPassword")
                            if(pwd && pwd.length > 0)
                            {
                                return pwd
                            }
                            return ''
                        }
                        placeholderText: qsTr('请输入密码')
                        echoMode: TextInput.Password
                        selectByMouse: true
                        anchors.verticalCenter: parent.verticalCenter
                        background: Item{}
                    }
                }
                Rectangle{
                    width: 160
                    height: 43
                    color: '#4cC2C2C2'
                    radius:5
                    Image {
                        width: 22
                        height: 22
                        source: "qrc:/images/login/yanzhengma.png"
                        anchors.verticalCenter: parent.verticalCenter
                        x:15
                    }
                    TextField{
                        id:textCode
                        x:50
                        height: parent.height-2
                        width: parent.width - x - 4
                        maximumLength:30
                        font.pixelSize: 14
                        color: '#333333'
                        placeholderText: qsTr('请输入验证码')
                        selectByMouse: true
                        anchors.verticalCenter: parent.verticalCenter
                        background: Item{}
                    }
                    MouseArea{
                        id:mouseCode
                        width: 150
                        height: parent.height
                        anchors.left: parent.right
                        anchors.leftMargin: 10
                        property string keyStr: ''
                        Image {
                            id: img
                            anchors.fill: parent
                            sourceSize: Qt.size(150, parent.height)
                            asynchronous: true
                            smooth: true
                        }
                        Component.onCompleted: {
                            loadCodePic()
                        }
                        onClicked: {
                            loadCodePic()
                        }

                        function loadCodePic()
                        {
                            mouseCode.keyStr=''
                            var request = HttpHelper.createIMRequest();
                            request.onSucceed( function(request){
                                 img.source = request.responseData.captcha
                                 mouseCode.keyStr=request.responseData.key
                            } );
                            request.onError(  function(request){
                                console.log('----/sys/captcha--error--',request.errorMessage)
                            } );
                            request.get("/sys/captcha")
                        }
                    }


                }
            }

            Button{
                id:btnLogin
                width: 320
                height: 43
                background: Rectangle{
                    color: parent.enabled ? '#1E728D' : 'gray'
                    radius: 5
                }
                contentItem:Item{
                    Text {
                        anchors.centerIn: parent
                        text: qsTr("登录")
                        color: 'white'
                        font.pixelSize: 16
                        font.bold: true
                    }
                }
                onClicked: {
                    if(textUserName.text.length === 0)
                    {
                        CGlobal.showMessage(window,qsTr('请输入账号'))
                        return
                    }
                    if( textPassword.text.length === 0)
                    {
                        CGlobal.showMessage(window,qsTr('请输入密码'))
                        return
                    }
                    if( textCode.text.length === 0)
                    {
                        CGlobal.showMessage(window,qsTr('请输入验证码'))
                        return
                    }
                    btnLogin.enabled=false
                    var request = HttpHelper.createIMRequest();
//                    request.enabledLog=true
                    request.onSucceed( function(request){
                        btnLogin.enabled=true
                        let userinfo=request.responseData.admin
                        userinfo['token']=request.responseData.token
                        UserHelper.userinfo=userinfo
                        CommonHelper.setValue("lastUser",textUserName.text)
                        CommonHelper.setValue("lastPassword",textPassword.text)
                        console.log('----login successed----',JSON.stringify(UserHelper.userinfo))
                        loginMain.loginSuccessed()
                    } );
                    request.onError(  function(request){
                        btnLogin.enabled=true
                        mouseCode.loadCodePic()
                        CGlobal.showMessage(window,request.errorMessage)
                    } );
                    request.post(
                                "/sys/login",
                                {
                                    key:mouseCode.keyStr,
                                    code:textCode.text,
                                    account: textUserName.text,
                                    password: textPassword.text
                                } )
                }

            }
        }
    }
    Image {
        anchors.right: parent.right
        anchors.rightMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 10
        source: "qrc:/images/login/code.png"
        Text {
            anchors.top: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            color: '#333333'
            text: qsTr("使用扫码枪请先扫描二维码切换成串口模式")
//            width: parent.width
//            wrapMode: Text.WrapAnywhere
            horizontalAlignment: Text.AlignHCenter
        }
    }
}
